//
//  CheckResultUserDefaults.m
//  MobileChecker
//
//  Created by Lymons on 15-1-6.
//  Copyright (c) 2015年 Lymons. All rights reserved.
//

#import "CheckResultUserDefaults.h"
#import <SharedInstanceGCD/SharedInstanceGCD.h>

@implementation CheckResultUserDefaults

- (id)init {
    self = [super init];
    if (self) {
        [self addObserverSelector:@selector(wifi3gCheckResult)];
        [self addObserverSelector:@selector(bluetoothCheckResult)];
        [self addObserverSelector:@selector(gpsCheckResult)];
        [self addObserverSelector:@selector(compassCheckResult)];
        [self addObserverSelector:@selector(magneticCheckResult)];
        [self addObserverSelector:@selector(proximityCheckResult)];
        [self addObserverSelector:@selector(luminaCheckResult)];
        [self addObserverSelector:@selector(vibrationCheckResult)];
        [self addObserverSelector:@selector(recordCheckResult)];
        [self addObserverSelector:@selector(soundCheckResult)];
        [self addObserverSelector:@selector(touchCheckResult)];
        [self addObserverSelector:@selector(screenCheckResult)];
        [self addObserverSelector:@selector(cameraCheckResult)];
        [self addObserverSelector:@selector(batteryCheckResult)];
        [self addObserverSelector:@selector(simCardCheckResult)];
        
        [self addObserverSelector:@selector(authCode)];
    }
    
    return self;
}

- (NSString *)prefixUserDefaultsKey {
    return @"";
}

+ (instancetype)standardUserDefaults {
    DEFINE_SHARED_INSTANCE_GCD_USING_BLOCK(^id{
        return [[CheckResultUserDefaults alloc] init];
    });
}

+ (NSString *)resultString:(DiagnosisCheckResult)result
{
    switch (result) {
        case DiagnosisCheckResultLost1:
            return ITEM_RESULT_LOST1;
            break;
        case DiagnosisCheckResultLost2:
            return ITEM_RESULT_LOST2;
            break;
        case DiagnosisCheckResultLost3:
            return ITEM_RESULT_LOST3;
            break;
        case DiagnosisCheckResultLost:
            return ITEM_RESULT_LOST;
            break;
        case DiagnosisCheckResultOk:
            return ITEM_RESULT_OK;
            break;
        case DiagnosisCheckResultNg:
            return ITEM_RESULT_NG;
            break;
        case DiagnosisCheckResultNoSupport:
            return ITEM_RESULT_NO_SUPPORT;
            break;
        case DiagnosisCheckResultUnknown:
            return ITEM_RESULT_UNKNOWN;
            break;
        case DiagnosisCheckResultNoCheck:
            return ITEM_RESULT_NO_CHECK;
            break;
        case DiagnosisCheckResultFound:
            return ITEM_RESULT_FOUND;
            break;
        case DiagnosisCheckResultNotFound:
            return ITEM_RESULT_NO_FOUND;
            break;
        default:
            break;
    }
    return ITEM_RESULT_NONE;
}

+ (NSString *)resultAPIStringForSim:(DiagnosisCheckResult)result
{
    switch (result) {
        case DiagnosisCheckResultFound:
            return API_ITEM_RESULT_SIM_OK;
            break;
        case DiagnosisCheckResultNotFound:
            return API_ITEM_RESULT_SIM_NO;
            break;
        default:
            break;
    }
    return API_ITEM_RESULT_SIM_NO_SUPPORT;
}

+ (NSString *)resultAPIString:(DiagnosisCheckResult)result
{
    switch (result) {
        case DiagnosisCheckResultLost1:
            return API_ITEM_RESULT_1;
            break;
        case DiagnosisCheckResultLost2:
            return API_ITEM_RESULT_2;
            break;
        case DiagnosisCheckResultLost3:
            return API_ITEM_RESULT_3;
            break;
        case DiagnosisCheckResultOk:
            return API_ITEM_RESULT_OK;
            break;
        case DiagnosisCheckResultNg:
            return API_ITEM_RESULT_NG;
            break;
        case DiagnosisCheckResultNoSupport:
            return API_ITEM_RESULT_NO_SUPPORT;
            break;
        case DiagnosisCheckResultUnknown:
            return API_ITEM_RESULT_UNKNOWN;
            break;
        default:
            break;
    }
    return API_ITEM_RESULT_UNKNOWN;
}

+ (NSString *)finalResultAPIString:(DiagnosisCheckResult)result
{
    switch (result) {
        case DiagnosisCheckResultLost1:
        case DiagnosisCheckResultLost2:
        case DiagnosisCheckResultLost3:
        case DiagnosisCheckResultLost:
            return API_RESULT_LOST;
            break;
        case DiagnosisCheckResultOk:
            return API_RESULT_OK;
            break;
        default:
            break;
    }
    return API_RESULT_NG;
}

+ (UIColor *)valueColorByResult:(DiagnosisCheckResult)result
{
    NSString *value = [CheckResultUserDefaults resultString:result];
    return [CheckResultUserDefaults valueColor:value];
}

+ (UIColor *)valueColor:(NSString *)value
{
    UIColor *textColor = nil;
    if ([value isEqualToString:ITEM_RESULT_NG]) {
        textColor = [UIColor redColor];
    } else if ([value isEqualToString:ITEM_RESULT_OK]) {
        textColor = [UIColor greenColor];
    } else if ([value isEqualToString:ITEM_RESULT_LOST1] || [value isEqualToString:ITEM_RESULT_LOST2] || [value isEqualToString:ITEM_RESULT_LOST3]) {
        textColor = [UIColor magentaColor];
    } else {
        textColor = [UIColor lightTextColor];
    }
    return textColor;
}

+ (DiagnosisCheckResult)sensorResult
{
    CheckResultUserDefaults *defaluts = [CheckResultUserDefaults standardUserDefaults];
    return (defaluts.compassCheckResult == DiagnosisCheckResultNg ||
            defaluts.magneticCheckResult == DiagnosisCheckResultNg ||
            defaluts.proximityCheckResult == DiagnosisCheckResultNg ||
            defaluts.luminaCheckResult == DiagnosisCheckResultNg) ? DiagnosisCheckResultNg : DiagnosisCheckResultOk;
}

+ (DiagnosisCheckResult)communicationResult
{
    CheckResultUserDefaults *defaluts = [CheckResultUserDefaults standardUserDefaults];
    return (defaluts.wifi3gCheckResult == DiagnosisCheckResultNg ||
            defaluts.bluetoothCheckResult == DiagnosisCheckResultNg) ? DiagnosisCheckResultNg : DiagnosisCheckResultOk;
}

+ (NSString *)sensorResultString
{
    return [CheckResultUserDefaults resultString:[CheckResultUserDefaults sensorResult]];
}

+ (NSString *)communicationResultString
{
    return [CheckResultUserDefaults resultString:[CheckResultUserDefaults communicationResult]];
}

+ (NSString *)sensorResultAPIString
{
    return [CheckResultUserDefaults resultAPIString:[CheckResultUserDefaults sensorResult]];
}

+ (NSString *)communicationResultAPIString
{
    return [CheckResultUserDefaults resultAPIString:[CheckResultUserDefaults communicationResult]];
}

+ (DiagnosisCheckResult)diagnosisResult
{
    CheckResultUserDefaults *defaluts = [CheckResultUserDefaults standardUserDefaults];
    if (defaluts.wifi3gCheckResult == DiagnosisCheckResultNg ||
        defaluts.bluetoothCheckResult == DiagnosisCheckResultNg ||
        defaluts.gpsCheckResult == DiagnosisCheckResultNg ||
        defaluts.compassCheckResult == DiagnosisCheckResultNg ||
        defaluts.magneticCheckResult == DiagnosisCheckResultNg ||
        defaluts.proximityCheckResult == DiagnosisCheckResultNg ||
        defaluts.luminaCheckResult == DiagnosisCheckResultNg ||
        defaluts.vibrationCheckResult == DiagnosisCheckResultNg ||
        defaluts.recordCheckResult == DiagnosisCheckResultNg ||
        defaluts.soundCheckResult == DiagnosisCheckResultNg ||
        defaluts.touchCheckResult == DiagnosisCheckResultNg ||
        defaluts.cameraCheckResult == DiagnosisCheckResultNg ||
        defaluts.batteryCheckResult == DiagnosisCheckResultNg ) {
        return DiagnosisCheckResultNg;
    }
    if (defaluts.screenCheckResult == DiagnosisCheckResultLost1 ||
        defaluts.screenCheckResult == DiagnosisCheckResultLost2 ||
        defaluts.screenCheckResult == DiagnosisCheckResultLost3) {
        return DiagnosisCheckResultLost;
    }
    return DiagnosisCheckResultOk;
}

+ (NSString *)diagnosisResultString
{
    return [CheckResultUserDefaults resultString:[CheckResultUserDefaults diagnosisResult]];
}

+ (NSString *)diagnosisResultAPIString 
{
    return [CheckResultUserDefaults finalResultAPIString:[CheckResultUserDefaults diagnosisResult]];
}

@end
